<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Order;

class OrderController extends Controller
{
    use ModelForm;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('订单');
            $content->description('管理');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('订单');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('订单');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(Order::class, function (Grid $grid) {
            // $grid->position_tags('轮播图序号');
            $grid->id('序号')->sortable();
            $grid->order_number('订单号');
            $grid->total_price('订单总价')->sortable();
            // $grid->user_name('下单用户名');
            $grid->name('用户名');
            $grid->phone_number('用户电话');
            // $grid->user_id('下单用户id');
            $grid->receiving_address('收货地址');
            $grid->leaving_message('订单留言信息');
            // $grid->member_preferences('会员优惠金额');

            //发货状态
            $delivery_status = [
            'on' => ['value' => '1', 'text' => '已发货', 'color' => 'success'],
            'off' => ['value' => '0', 'text' => '未发货', 'color' => 'danger'],
            ];
            $grid->column('delivery_status','发货状态')->switch($delivery_status);
            //付款状态
            $payment_status = [
            'on' => ['value' => '1', 'text' => '已付款', 'color' => 'success'],
            'off' => ['value' => '0', 'text' => '未付款', 'color' => 'danger'],
            ];
            // $grid->display('payment_status','发货状态')->switch($payment_status);
            $grid->column('payment_status','付款状态')->switch($payment_status);
            // 测试  S
            // 测试  E
            // $grid->disableFilter();
            // $grid->disableExport();
            // $grid->disableCreateButton();
            // $grid->disablePagination();
            $grid->disableFilter();
            $grid->paginate(10);
            $grid->perPages([10,20,30]);
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->actions(function ($actions) {
                // $actions->disableDelete();
                $actions->disableDelete();
            });
        });
    }

    protected function form()
    {
        return Admin::form(Order::class, function (Form $form) {
            // $form->text('position_tags', '轮播图位置');
            $form->text('order_number', '订单号');
            $form->text('total_price', '订单总价');
            // $form->text('user_name', '下单用户名');
            // $form->text('user_id', '下单用户id');
            $form->text('receiving_address', '收货地址');
            $form->text('leaving_message', '订单留言信息');
            $form->text('name', '用户名');
            $form->text('phone_number', '用户电话');
            $form->text('member_preferences', '会员优惠金额');
            // 发货状态
            $delivery_status = [
  'on'  => ['value' => 1, 'text' => '已发货', 'color' => 'success'],
  'off' => ['value' => 0, 'text' => '未发货', 'color' => 'danger'],
  ];
            $form->switch("delivery_status","发货状态")->states($delivery_status);

            //付款状态
            $payment_status = [
            'on' => ['value' => '1', 'text' => '已付款', 'color' => 'success'],
            'off' => ['value' => '0', 'text' => '未付款', 'color' => 'danger'],
            ];
            $form->switch("payment_status","付款状态")->states($payment_status);
            // $form->display('id', 'ID')
            //
        });
    }
  }
